<script>
    class EventEmitter {


  constructor() {
    this.eventList = {
      // 'hasHouse': [kang, wrap],
      // 'hasCar': [cheng]
    }
  }

  on(eventName, cb) {
    if (!this.eventList[eventName]) {
      this.eventList[eventName] = []
    }

    this.eventList[eventName].push(cb)
  }
  emit(eventName) {
    if (this.eventList[eventName]) {
      const handlers = this.eventList[eventName].slice()   // 
      handlers.forEach((item) => {
        item()
      })
    }
  }
  off(eventName, cb) {
    const callbacks = this.eventList[eventName]
    const index = callbacks.indexOf(cb)
    if (index !== -1) {
      callbacks.splice(index, 1)
    }
  }
  once(eventName, cb) {
    const wrap = () => {
      cb()
      this.off(eventName, wrap)
    }
    this.on(eventName, wrap)
  }
}

const eventEmitter = new EventEmitter()
function fnA(){    
    setTimeout(() => {
        console.log('fnA 执行了')
        eventEmitter.emit('next')
    }, 1000)
}
function fnB(){
    setTimeout(() => {
        console.log('fnB 执行了')
    }, 500)
}


eventEmitter.on('next',fnB)
fnA()
</script>